/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-12-01     周童欢       the first version
 */
#include "board.h"
#include <rtdevice.h>
#include "gpio.h"
#define TAS5825_I2C_BUS_NAME  "i2c1"
#define TAS5825_I2C_BUS_ADDR  0x4E
#define TAS5825_VAL_DIE_ID    0x95
#define TAS5825_REG_DIE_ID    0x67
struct rt_i2c_bus_device *tas5825_iic  = RT_NULL;

/* i2c read reg */
static rt_err_t iic_read_reg_8(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t len, rt_uint8_t *buf)
{
    RT_ASSERT(bus != RT_NULL);
    struct rt_i2c_msg msg[2] = {0};

    msg[0].addr  = TAS5825_I2C_BUS_ADDR;
    msg[0].flags = RT_I2C_WR;
    msg[0].buf   = &reg;
    msg[0].len   = 1;

    msg[1].addr  = TAS5825_I2C_BUS_ADDR;
    msg[1].flags = RT_I2C_RD;
    msg[1].buf   = buf;
    msg[1].len   = len;

    if (rt_i2c_transfer(bus, msg, 2) == 2)
    {
        return RT_EOK;
    }
    else {
        rt_kprintf("IIC read error\r\n");
    }

    return -RT_ERROR;
}
/* i2c write reg */
static rt_err_t iic_write_reg_8(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t data)
{
    RT_ASSERT(bus != RT_NULL);
    rt_uint8_t buf[2];
    struct rt_i2c_msg msgs;

    buf[0] = reg;
    buf[1] = data;

    msgs.addr = TAS5825_I2C_BUS_ADDR;
    msgs.flags = RT_I2C_WR;
    msgs.buf = buf;
    msgs.len = 2;

    if (rt_i2c_transfer(bus, &msgs, 1) == 1)
    {
        return RT_EOK;
    }

    return -RT_ERROR;
}

static rt_uint16_t tas5825_read_id(struct rt_i2c_bus_device *bus)
{
    rt_uint8_t ID;

    iic_read_reg_8(bus, TAS5825_REG_DIE_ID, 1, &ID);

    if (ID != TAS5825_VAL_DIE_ID)
    {
        rt_kprintf("tas5825 init error, id: 0x%x\r\n", ID);
        return -RT_ERROR;
    }

    return RT_EOK;
}
static void tas5825_iic_init(void)
{
    tas5825_iic = (struct rt_i2c_bus_device *)rt_device_find(TAS5825_I2C_BUS_NAME);
    if (tas5825_iic == RT_NULL)
    {
        rt_kprintf("can't find %s device!\n", TAS5825_I2C_BUS_NAME);
        return;
    }
}

#include <stdio.h>
#include <stdlib.h>
static void iic_read(int argc, char *argv[])
{
//    iic_read_reg_8(tas5825_iic, reg, 1, &data);
    char *endptr;
    uint8_t reg, data;

    if (argc == 2)
    {
        reg  = strtol(argv[1], &endptr,  16);
        rt_kprintf("input reg:0x%x \n", reg);
    }
    else {
        rt_kprintf("iic_read error! please check your input\n");
        return;
    }

    uint8_t ret = iic_read_reg_8(tas5825_iic, reg, 1, &data);
    if(ret)
    {
        rt_kprintf("%s %d:iic_read failed! reg:0x%x val:0x%x\n", __func__, __LINE__, reg, data);
    }
    else {
        iic_read_reg_8(tas5825_iic, reg, 1, &data);
        rt_kprintf("iic_read OK! reg:0x%x val:0x%x ret:%d\n", reg, data, ret);
    }
}

static void iic_write(int argc, char *argv[])
{
    char *endptr;
    uint8_t reg, data;

    if (argc == 3)
    {
        reg  = strtol(argv[1], &endptr,  16);
        data = strtol(argv[2], &endptr,  16);
        rt_kprintf("input reg:0x%x data:0x%x\n", reg, data);
    }
    else {
        rt_kprintf("iic_write error! please check your input\n");
        return;
    }

    uint8_t ret = iic_write_reg_8(tas5825_iic, reg, data);
    if(ret)
    {
        rt_kprintf("%s %d:iic_write failed! reg:0x%x val:0x%x\n", __func__, __LINE__, reg, data);
    }
    else {
        iic_read_reg_8(tas5825_iic, reg, 1, &data);
        rt_kprintf("iic_write OK! reg:0x%x val:0x%x ret:%d\n", reg, data, ret);
    }
}


int rt_tas5825_init(void)
{
    uint8_t ret;
    tas5825_iic_init();
//    while(1)
//    {
//        ret = tas5825_read_id(tas5825_iic);
//        rt_thread_delay(10);
//    }
    rt_thread_delay(100);
    ret = tas5825_read_id(tas5825_iic);
    if(ret)
    {
        rt_kprintf("fail at %s %d\n", __func__, __LINE__);
    }

    iic_write_reg_8(tas5825_iic, 0x03, 0x03);
    iic_write_reg_8(tas5825_iic, 0x54, 0x15);
//    uint8_t data;
//    uint8_t reg = 0x37;
//    iic_read_reg_8(tas5825_iic, reg, 1, &data);
//    rt_kprintf("can't find %s device!\n", data);

    return RT_EOK;
}
//INIT_APP_EXPORT(rt_tas5825_init);
MSH_CMD_EXPORT(iic_write, iic_write);
MSH_CMD_EXPORT(iic_read, iic_read);

